package com.xy.test.service.impl;
import com.xy.test.mapper.StockMapper;
import com.xy.test.service.StockService;
import com.xy.test.service.TccStockService;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TccStockServiceImpl implements TccStockService {

    @Autowired
    private StockMapper stockMapper;


    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void reduceTcc(Integer productId) {
        System.out.println("seata reduceTcc 全局事务id====================>"+ RootContext.getXID());
        System.out.println("stock 接收到："+productId);
        stockMapper.stockDesc(productId);
        //本地事务
        //throw new RuntimeException("123");
    }

    @Override
    public boolean reduceTccCommit(BusinessActionContext context) {
        return true;
    }

    @Override
    public boolean reduceTccRollback(BusinessActionContext context) {
        System.out.println("seata reduceTccRollback 全局事务id====================>"+ context.getXid());
        Object prod = context.getActionContext("productId");
        System.out.println("reduceTccRollback 接收到："+prod);
        stockMapper.restoreStockDesc(Integer.valueOf(prod.toString()));
        return true;
    }
}